/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sysno2signature;

import cz.mzk.oai.metadataRepository.repository.Repository;
import cz.mzk.oai.metadataRepository.tools.CannotReadDirectoryException;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author martin
 */
public class Main {

    private static String usage = "Pouziti: 'java -jar sysno2signature.jar "
            + "analyze baze_alephu cesta_k_transformacni_tabulce |\n"
            + "rename baze_alephu cesta_k_transformacni_tabulce |\n"
            + "create_table baze_alephu\n"
            + "Toto se musi zavolat v adresari, kde jsou xml soubory (jinak by parserovi chybelo dtd)";
    private static File CURRENT_DIR = new File(".");

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        //File dir = new File("/home/martin/tmp/XML_2_SVN/Moll_Rkp");
        if (args.length != 2) {
            System.err.println(usage);
            return;
        }
        checkArgs(args, 3);
        String command = args[0];
        String alephBase = args[1];
        File mapTable = new File(args[2]);
        //File table = new File("/home/martin/oai/transformationTable-mzk03");

        if ("analyze".equals(command)) {
            analyze(alephBase, mapTable);
        } else if ("rename".equals(command)) {
            rename(alephBase, mapTable);
        } else if ("create_table".equals(command)) {
            createMapTable(alephBase, mapTable);
        } else {
            System.err.println(usage);
            return;
        }
    }

    private static void checkArgs(String[] args, int requiredNumber) {
        if (args.length != requiredNumber) {
            throw new IllegalArgumentException("Spatny pocet argumentu - " + args.length);
        }
    }

    private static void rename(String alephBase, File tableFile) {
        try {
            FileRenamer renamer = new FileRenamer(CURRENT_DIR);
            Converter converter = new Converter(tableFile, alephBase);
            renamer.rename(converter);
        } catch (CannotReadDirectoryException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        } catch (FileNotFoundException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private static void analyze(String alephBase, File tableFile) {
        try {
            FileRenamer renamer = new FileRenamer(CURRENT_DIR);
            Converter converter = new Converter(tableFile, alephBase);
            System.out.println("analyzing");
            renamer.analyze(converter);
        } catch (CannotReadDirectoryException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        } catch (FileNotFoundException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private static void createMapTable(String alephBase, File tableFile) {
        File propertiesFile = new File("local.properties");
        //System.out.println("properties file: " + propertiesFile.getAbsolutePath());
        try {
            InputStreamReader reader = new FileReader(propertiesFile);
            Properties properties = new Properties();
            properties.load(reader);
            Repository repository = Repository.instanceOf(properties);
            Converter.createTransformationTable(repository, alephBase, tableFile);
        } catch (FileNotFoundException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, "Could not find file " + propertiesFile.getAbsolutePath(), ex);
            System.err.println(usage);
            return;
        } catch (IOException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, "Error while reading file " + propertiesFile.getAbsolutePath(), ex);
            return;
        }
    }
}
